8 Replies - 952 Views - Last Post: 15 July 2015 - 12:10 AM Rate Topic: -----

#1 mukesh600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 15

php query is not working

Posted 13 July 2015 - 12:29 PM

Please help me regarding this, I am sharing the code for my index.php and connect.inc.php , I am getting error as
"Warning: mysqli_query() expects at least 2 parameters, 1 given in " and when I am trying to add the $connection to mysqli_query like $query_run = mysqli_query($connection,$query); I am getting error as "Notice: Undefined variable: connection in " and "Warning: mysqli_query() expects parameter 1 to be mysqli, null given in"

My connect.inc.php file code is

	
        <?php

	$conn_error='Could not connect';
	$mysql_host = 'localhost';
	$mysql_user='root';
	$mysql_pass='root';
	$mysql_db='a_database';
	$connection=mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db) ;

	 if(!@mysqli_connect($mysql_host,$mysql_user,$mysql_pass) and!@mysqli_select_db($connection,$mysql_db)){
		 die($conn_error);
	 }
	?>


      

and my index.php code is


<?php
require 'connect.inc.php';
$user_ip=$_SERVER['REMOTE_ADDR'];

function ip_exists($ip){
	global $user_ip;
	
}
function update_count(){
	$query="SELECT `count` FROM `hits_count`";
	
$query_run = mysqli_query($connection,$query);
	}
update_count();
?>



Is This A Good Question/Topic? 0
  • +

Replies To: php query is not working

#2 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 602
  • Joined: 19-May 14

Re: php query is not working

Posted 13 July 2015 - 12:48 PM

I usually work with pdo but you could try doing it this way:

$result = $mysqli->query($query);


were mysqli is the connection

Hey wait a second. i just noticed you have your variables inside an function so Try this:


function update_count(){
    global $connection;

    $query="SELECT `count` FROM `hits_count`";
    $query_run = mysqli_query($connection,$query);
}



check this link for more detailed explanation:
http://php.net/manua...ables.scope.php

This post has been edited by RamonRobben: 13 July 2015 - 12:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: php query is not working

Posted 14 July 2015 - 12:10 AM

Quote

i just noticed you have your variables inside an function so Try this:

please don’t try this! using globals is a debugging nightmare and violates good programming practices.

two better alternatives:
function update_count($connection)
{
    $query="SELECT `count` FROM `hits_count`";
    return mysqli_query($connection, $query);
}



function update_count() use ($connection)
{
    $query="SELECT `count` FROM `hits_count`";
    return mysqli_query($connection, $query);
}



or without a function:

$result = $connection->query("SELECT `count` FROM `hits_count`");


This post has been edited by Dormilich: 14 July 2015 - 12:11 AM

Was This Post Helpful? 2
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: php query is not working

Posted 14 July 2015 - 12:16 AM

Additionally:
- never ever use error suppression
- mysqli_connect() is never false. connection failures you have to test explicitly with $connection->connect_error
- there is no need to use mysqli_select_db(), you can pass the database name as fourth parameter to the constructor
Was This Post Helpful? 1
  • +
  • -

#5 mukesh600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 15

Re: php query is not working

Posted 14 July 2015 - 11:31 AM

Yes thanks the global works but now I have changed my code a bit and now I am getting error as
"( ! ) Warning: mysql_result() expects parameter 1 to be resource, object given in C:\wamp\www\series\database_hit_counter1\index.php on line 14" my connect.inc.php is same.Please help.

<?php
require 'connect.inc.php';
$user_ip=$_SERVER['REMOTE_ADDR'];

function ip_exists($ip){
	global $user_ip;
	
}
function update_count(){
	global $connection;
	$query="SELECT `count` FROM `hits_count`";
	
if(@$query_run = mysqli_query($connection,$query)){
	$count= mysql_result($query_run,0,'count');
	echo $count;
}
	}
update_count();
?>



Thanks for your help but I tried what you have suggested but that was not working .
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: php query is not working

Posted 14 July 2015 - 11:36 AM

Why are you taking the bad advice, but not the good advice? And why are you using @ to hide error messages? If errors happen you want to just ignore them?

About your error message, mysql_result does not work with mysqli_query.
Was This Post Helpful? 0
  • +
  • -

#7 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 602
  • Joined: 19-May 14

Re: php query is not working

Posted 14 July 2015 - 12:11 PM

This is the proper way of getting results from query:

edit: last comment provides right answer.

and in your function please follow Dormilich advise since it better.

This post has been edited by RamonRobben: 14 July 2015 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#8 mukesh600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 15

Re: php query is not working

Posted 14 July 2015 - 11:59 PM

You saying that mysql_result does not work with mysqli_query then how to solve this , how will it work with mysqli_query, what could I use instead ?
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: php query is not working

Posted 15 July 2015 - 12:10 AM

use one of the fetch() methods. e.g. $count = $result->fetch_array()[0];.

admittedly with PDO thatís a bit more straightforward Ö
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1