chaking Posted February 16, 2008 Share Posted February 16, 2008 Very new to writing php and I'm trying to work through the errors but I don't know what I'm doing wrong here: $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die('Couldnt connect to server'); $primary_email = 'SELECT email FROM responders WHERE (id="a2z_primary")'; $result_first = mysqli_query($cxn,$primary_email) or die('Couldn’t execute query'); $primary_responder = 'SELECT * FROM contacts WHERE (email="$result_first")'; $result = mysqli_query($cxn,$primary_responder) or die("Couldn't execute query2"); $row = mysqli_fetch_array($result); extract($row); The error I receive is: Warning: extract() [function.extract]: First argument should be an array in E:\xampp-win32-1.6.5\xampp\htdocs\amazon\a2z.php on line 42 It doesn't look like $row is actually holding any information - in fact if I make an if statement of: if ($row == "") {echo "nothing here"} and put it before the last line in the code, then I get "nothing here" - Any help would be very much appreciated, thanks - Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 Try making you code cleaner: <?php $cxn = mysqli_connect($host, $user, $passwd, $dbname) or die(mysqli_error()); $primary_email = "SELECT `email` FROM `responders` WHERE `id` = 'a2z_primary'"; $result_first = mysqli_query($cxn, $primary_email) or die(mysqli_error()); $primary_responder = "SELECT * FROM `contacts` WHERE `email` = '$result_first'"; $result = mysqli_query($cxn, $primary_responder) or die(mysqli_error()); $row = mysqli_fetch_array($result); if ( empty($row) ) { die("nothing found in the database"); } extract($row); ?> Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 You might be better of getting the content into an array aswell: $primary_email = "SELECT `email` FROM `responders` WHERE `id` = 'a2z_primary'"; $result_first = mysqli_query($cxn, $primary_email) or die(mysqli_error()); $array_first = mysqli_fetch_assoc($result_first); $primary_responder = "SELECT * FROM `contacts` WHERE `email` = '{$array_first['email']}'"; $result = mysqli_query($cxn, $primary_responder) or die(mysqli_error()); $array = mysqli_fetch_assoc($result); Quote Link to comment Share on other sites More sharing options...
chaking Posted February 16, 2008 Author Share Posted February 16, 2008 Thanks for the quick reply chris - Unfortunately I'm getting the last die error - nothing found in the database... With this code: $cxn = mysqli_connect($host, $user, $passwd, $dbname) or die(mysqli_error()); $primary_email = "SELECT `email` FROM `responders` WHERE `id` = 'a2z_primary'"; $result_first = mysqli_query($cxn, $primary_email) or die(mysqli_error()); $array_first = mysqli_fetch_assoc($result_first); $primary_responder = "SELECT * FROM `contacts` WHERE `email` = '{$array_first['email']}'"; $result = mysqli_query($cxn, $primary_responder) or die(mysqli_error()); $array = mysqli_fetch_assoc($result); $row = mysqli_fetch_array($array); if ( empty($row) ) { die("nothing found in the database"); } else { extract($row); } I receive this error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in E:\xampp-win32-1.6.5\xampp\htdocs\amazon\a2z.php on line 42 nothing found in the database Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 Oh, my bad, i didn;t see that you had already put it into an array, im just thick $cxn = mysqli_connect($host, $user, $passwd, $dbname) or die(mysqli_error()); $primary_email = "SELECT `email` FROM `responders` WHERE `id` = 'a2z_primary'"; $result_first = mysqli_query($cxn, $primary_email) or die(mysqli_error()); $array_first = mysqli_fetch_assoc($result_first); $primary_responder = "SELECT * FROM `contacts` WHERE `email` = '{$array_first['email']}'"; $result = mysqli_query($cxn, $primary_responder) or die(mysqli_error()); $row = mysqli_fetch_assoc($result); if ( empty($row) ) { die("nothing found in the database"); } else { extract($row); } Quote Link to comment Share on other sites More sharing options...
chaking Posted February 16, 2008 Author Share Posted February 16, 2008 ah - thanks so much! It worked =) A n00b's joy is still joy Quote Link to comment Share on other sites More sharing options...
chaking Posted February 19, 2008 Author Share Posted February 19, 2008 Ah ok - I ran into another problem I'm not seeing an immediate answer to: I try to pass the variable through the url like this: test.php?bldg=A2Z Then I write this: [pre]<? $bldg_current = $_GET['bldg']; $cxn = mysqli_connect($host, $user, $passwd, $dbname) or die(mysqli_error()); $facilitycode = "SELECT * FROM 'facilities' WHERE 'facility_code' = '$bldg_current'"; $result_facilitycode = mysqli_query($cxn, $facilitycode) or die(mysqli_error()); $array_facilitycode = mysqli_fetch_assoc($result_facilitycode); if ( empty($array_facilitycode) ) { die("nothing found in the database"); } else { extract($array_facilitycode); } ?>[/pre] My error is coming up: Warning: mysqli_error() expects exactly 1 parameter, 0 given It's referring to this line: $result_facilitycode = mysqli_query($cxn, $facilitycode) or die(mysqli_error()); I guess the $facilitycode isn't passing any information? Any help would ver much be appreciated - thanks Quote Link to comment Share on other sites More sharing options...
chaking Posted February 19, 2008 Author Share Posted February 19, 2008 I'll give 5 bucks to anyone that can help me =)) Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 20, 2008 Share Posted February 20, 2008 Look at the error: mysqli_error() expects exactly 1 parameter, 0 given Look at your line causing the error: $result_facilitycode = mysqli_query($cxn, $facilitycode) or die(mysqli_error()); Look at your call to mysqli_error: die(mysqli_error()); How many parameters did you pass to your call to mysqli_error()? *Hint* You didn't pass any, but we already knew that from the error message: Warning: mysqli_error() expects exactly 1 parameter, 0 given So look at the documentation for mysqli_error() and find out what it expects as its first argument and then supply it. I'd guess that it's your $cxn variable. Quote Link to comment Share on other sites More sharing options...
chaking Posted February 20, 2008 Author Share Posted February 20, 2008 I'm sorry, I don't quite understand why the mysqli_error function would need a parameter? I thought it's function was merely to print the error message. So, instead of putting "die" before the mysqli_error (), I put "print" --- I read that this will let it continue and show what's going on... I now get this Warning: Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in.... And then it refers to this line of code: $array_facilitycode = mysqli_fetch_assoc($result_facilitycode); Does this mean I'm receiving a true or false statement instead of an array? Possibly because it's not matching my query with a valid result? So I guess my question would be: How am I formatting the request wrong? Because I know the values match the values in the db, so if it can match it in the db it must be my formatting. You mentioned that maybe the $cxn was the problem? But I use the exact same $cxn settings in another page and it works fine. I'm sorry to be a pain, but I can't help it =/ Quote Link to comment Share on other sites More sharing options...
trq Posted February 20, 2008 Share Posted February 20, 2008 Read roopurt's reply again. It explains (in great detail I might add) how you yourself can eliminate your own errors by simply learning how to read the error messages. To help you out though, mysqli_query is failing because you query is ppoorly formed. There should be no quotes around field names. $facilitycode = "SELECT * FROM facilities WHERE facility_code = '$bldg_current'"; Quote Link to comment Share on other sites More sharing options...
chaking Posted February 20, 2008 Author Share Posted February 20, 2008 Thank you thorpe - I seriously read roopurt's reply about 20 times. I'm sorry, but I don't understand php completely yet and I didn't understand his reply even after reading it thoroughly. Also, I didn't think the quotes were problematic because in Chris' response above, he gave me code with quotes around the field names and the code worked. But you were absolutely right, I took them out and now it functions as it is supposed to. So thank you, and I'm sorry I'm dumb - but hey, I'm dumb, what can I do? Also, in the 3 books I've read to try and learn php (O'Reilly, PHP For Dummies, and ... ahh I forget the name) they seem to put single quotes around the field names. This is why I get so confused, a quote here or there can throw it all off. I should learn how to read errors better, but they are confusing to me for now. Thanks anyway Quote Link to comment Share on other sites More sharing options...
chaking Posted February 20, 2008 Author Share Posted February 20, 2008 Oh - well now I see what's wrong - This: ' Does NOT equal This: ` Never even realized that was that before Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 20, 2008 Share Posted February 20, 2008 You're not stupid, just inexperienced. But learning how to read and decipher error messages will go a long way in helping you fix problems. And always, always make a good, honest attempt at figuring out what the problem you're having is. Not saying you didn't do that here, but by doing so you rack up experience points in regards to troubleshooting. If you can train your eye to notice subtle syntax errors and other things, you make those mistakes less or notice them quicker in the future. Don't feel bad, every programmer has lost a significant portion of their time looking for what turned out to be a misplaced semicolon or a lowercase letter instead of an uppercase one. Quote Link to comment 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.