purplemonkey Posted September 22, 2010 Share Posted September 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/ Share on other sites More sharing options...
DavidAM Posted September 22, 2010 Share Posted September 22, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114247 Share on other sites More sharing options...
Mchl Posted September 22, 2010 Share Posted September 22, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114271 Share on other sites More sharing options...
DavidAM Posted September 23, 2010 Share Posted September 23, 2010 And in mysqli it's first and required. I really advise sticking to mysqli. The database is selected in mysqli_connect Hmm, I learn something new every day. At any rate, you definitely can't mix the two. Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114356 Share on other sites More sharing options...
purplemonkey Posted September 23, 2010 Author Share Posted September 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114395 Share on other sites More sharing options...
Mchl Posted September 23, 2010 Share Posted September 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114396 Share on other sites More sharing options...
purplemonkey Posted September 23, 2010 Author Share Posted September 23, 2010 I guess i should read the error messages hey sorry was trying it out this morning before work. Will check out the suggestion later. hopefully one day soon all this code will start making sense to me, then I can stop the cut and paste method of coding I seem to be using now adays! Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114425 Share on other sites More sharing options...
Mchl Posted September 23, 2010 Share Posted September 23, 2010 Bookmark php.net and visit it often. Even if you think you know some function well, it might be insightful to read about it manual. Quote Link to comment https://forums.phpfreaks.com/topic/214129-cant-run-query/#findComment-1114495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.