Jump to content

can't run query?


purplemonkey

Recommended Posts

hi, I've been trying a new query (one that isn't in my php book!) I found this query online, in the php help files etc.. now the problem comes down to a database connection, which Im using based on advice from my book. I can fix the issue but I can't work out why one works on not the other, explanation or advice would be really appricated.

 

My queries work by having an external file which I include, this declares all the db stuff.

external file.(mysqli_connect.php)

 
...
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('could not connect to mysql: '. mysqli_connect_error() );

 

I then build my query

           
require_once ('mysqli_connect.php');
$q = "Select * FROM table";
$result = mysql_query($dbc, $q);

 

if I try to use my new found command, it doesn't work.

$i = 0;
while ($i < mysql_num_fields($result)) {
}

 

 

to fix this I have to alter the way I connect to the DB, and I drop the dbc bit as follows.

$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');

mysql_select_db('database');
$r = mysql_query('select * from table');

 

the error I get is

Warning: mysql_query() expects parameter 1 to be string, object given in

hope that makes sense, if you want more info let me know.

Link to comment
Share on other sites

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)

You are using mysqli for the connection but then using mysql (no i) functions. You cannot mix the two. Either use mysql_connect or change the others to mysqli functions.

 

$result = mysql_query($dbc, $q);

The database connection parameter is the second parameter (and it is optional). Either leave it out, or trade $q and $dbc in this function call. This is what the error message is telling you.

 

mysql_select_db('database');

In your included script, you did not select a database. You need to select the database or qualify all of your table names in all of your queries.  Usually, we select the database immediately after connecting to the server.

 

 

 

Link to comment
Share on other sites

$result = mysql_query($dbc, $q);

The database connection parameter is the second parameter (and it is optional). Either leave it out, or trade $q and $dbc in this function call. This is what the error message is telling you.

 

And in mysqli it's first and required. I really advise sticking to mysqli.

 

The database is selected in mysqli_connect

Link to comment
Share on other sites

Thanks for pointing out the mismatch with mysql and mysqli, if you have the time and inclination could you explain the difference? I'll have a search on the net in the mean time. :)

 

more importantly, I'm still unable to get this working with my external db connection script. I now have the following setup.

external db file

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('could not connect to mysql: '. mysqli_connect_error() );

script

		require_once ('/includes/mysqli_connect.php');
		$q = "Select * FROM table";
		$r = mysqli_query($dbc, $q);
		$i = 0;
		while ($i < mysqli_num_fields($r)) {
			echo "Information for column $i:<br />\n";
			$meta = mysqli_fetch_field($r, $i);
			if (!$meta) {
				echo "No information available<br />\n";
			} 
			echo "name:         $meta->name";
$i++;
		}

 

now my error is

Warning: mysqli_fetch_field() expects exactly 1 parameter, 2 given in C:\xampp\xampp\htdocs\xxxx on line 22

Link to comment
Share on other sites

Chenage

    

$meta = mysqli_fetch_field($r, $i);

to

$meta = mysqli_fetch_field($r);

(each call of this function will fetch information about next column)

 

In general mysqli_ functions are a recommended way of working with MySQL server versions newer than 4.0. It can use mysqlnd native driver (snce PHP 5.3 which can give some performance gain). It has a built in support for transactions, prepared statements and other features introduced in MySQL 5.x. It provides a slightly better API, so that you're less prone to create silly bugs. Last but not least it provides an object oriented interface, which can be extended for better code reusabilty.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.