farkewie Posted September 8, 2007 Share Posted September 8, 2007 Hello i am trying to create a search page where i have a database with a table called "phones" in that table i have columns; id, make, model, userid , IMEI. my form has i text box "id=search" form action "search.php" method "get". i want to be able to use part word search but for now i am at least trying to get anything working. so im trying to type motorola v6 in the text box and echo the make and model columns that match either both words or one or the other. here is the error i cant seem to get past, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource here is my search.php <?php error_reporting(E_ALL); ini_set('display_errors', true); include('Connections/mobiles.php'); ?> <?php $search = $_GET['search']; // testing to make sure i am getting details from search form ( working) echo ("$search"); // connecting to the database $connection = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); //selecting the database $db = mysql_select_db($database_mobiles); // searching table $sql = "SELECT FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; $phoneRow = mysql_fetch_array($sql); $resultmake = $phoneRow['make']; $resultmodel = $phoneRow['model']; // display the phones echo ("$resultmake"); echo ("$resultmodel"); ?> any tips at all would be great Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/ Share on other sites More sharing options...
cooldude832 Posted September 8, 2007 Share Posted September 8, 2007 $sql = "SELECT FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; $phoneRow = mysql_fetch_array($sql); That doesn't work $sql is a string u need to say something like this $sql = "SELECT FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; $result = mysql_query($sql) or die(mysql_error()); $phoneRow = mysql_fetch_array($result); Now if you have more than 1 row you need a loop to do it right. Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344098 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 Thank you for your fast reply!! i have changed like you said , im not sure how to loop im only new but ill google that when i get rid of these errors, im getting a new error now. vYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM phones WHERE make LIKE 'Motorola v6' AND model LIKE 'Motorola v6'' at line 1 here is my new code: <?php error_reporting(E_ALL); ini_set('display_errors', true); include('Connections/mobiles.php'); ?> <?php $search = $_GET['search']; // testing to make sure i am getting details from search form ( working) echo ("$search"); // connecting to the database $connection = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); //selecting the database $db = mysql_select_db($database_mobiles); // searching table $sql = "SELECT FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; $result = mysql_query($sql) or die(mysql_error()); $phoneRow = mysql_fetch_array($result); $resultmake = $phoneRow['make']; $resultmodel = $phoneRow['model']; // display the phones echo ("$resultmake"); echo ("$resultmodel"); ?> i can never seem to get a query working when im going for multiple columns or tables, Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344100 Share on other sites More sharing options...
darkfreaks Posted September 8, 2007 Share Posted September 8, 2007 <?php sql = "SELECT *FROM 'phones' WHERE 'make' LIKE '". $_GET['search']. "' AND 'model' LIKE '". $_GET['search']. "' "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344143 Share on other sites More sharing options...
marcus Posted September 8, 2007 Share Posted September 8, 2007 $search = mysql_real_escape_string($_GET[search]); $sql = "SELECT * FROM `phones` WHERE `make` LIKE '%$search%' OR `model` LIKE '%$search%'"; $result = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344144 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 Thank you both for replies, i am now getting another error Use of undefined constant search - assumed 'search' in F:\www\xampp\htdocs\phone\test\test_result.php on line 7 herer is the whole code now : <?php error_reporting(E_ALL); ini_set('display_errors', true); include('Connections/mobiles.php'); ?> <?php $search = mysql_real_escape_string($_GET[search]); // testing to make sure i am getting details from search form ( working) echo ("$search"); // connecting to the database $connection = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); //selecting the database $db = mysql_select_db($database_mobiles); // searching table $sql = "SELECT * FROM `phones` WHERE `make` LIKE '%$search%' OR `model` LIKE '%$search%'"; $result = mysql_query($sql) or die(mysql_error()); $phoneRow = mysql_fetch_array($result); $resultmake = $phoneRow['make']; $resultmodel = $phoneRow['model']; // display the phones echo ("$resultmake"); echo ("$resultmodel"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344145 Share on other sites More sharing options...
darkfreaks Posted September 8, 2007 Share Posted September 8, 2007 <?php $search = mysql_real_escape_string($_GET['search']);?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344146 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 Now its perfect thank you so much.. only other thing i am having trouble with is i have access levels setup and "1" = User and "3" = Admin i amd trying to echo out what a user in the database is i have never used "if" or "else" statments before this was my attempt. but it always just prints "adminuser" looks like its not checking and just printing both? <?php $ul = $row_userinfo['level']; if ($ul = ("1")); { echo ("User"); } if ($ul = ("3")); { echo ("Admin"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344148 Share on other sites More sharing options...
darkfreaks Posted September 8, 2007 Share Posted September 8, 2007 <?php $ul = $row_userinfo['level']; if ($ul =="1");{ echo "User";} if ($ul == "3");{ echo "Admin";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344151 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 it is still doing the exact same thing? but if i echo "$ul" it returns a "1" or "3" depending on the level so it is reading the database and setting the variable.. ive tried to look through the php manual but im getting stumped.. Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344156 Share on other sites More sharing options...
darkfreaks Posted September 8, 2007 Share Posted September 8, 2007 im confused what do u want it to do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344157 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 Sorry i am displaying the user information out of an existing database, ie; name, userid, workstream, level, but i dont want the level displayed as a 1 or a 3 i want it too say admin or user ie an admin will see there level as the work "admin" instead of "3" i cant go hanging the database because my whole site checks for 1 or 3. Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344158 Share on other sites More sharing options...
darkfreaks Posted September 8, 2007 Share Posted September 8, 2007 <?php $row_userinfo=mysql_fetch_array($sql); if ($row_userinfo['level']=="1"){ echo "User";} if ($row_userinfo['level']=="3"){ echo "Admin";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344160 Share on other sites More sharing options...
marcus Posted September 8, 2007 Share Posted September 8, 2007 Why not use a switch for that. switch($row_userinfo['level']){ case 1: $level = "User"; break; case 3: $level = "Admin"; break; default: $level = "User"; } echo $level; Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344164 Share on other sites More sharing options...
farkewie Posted September 8, 2007 Author Share Posted September 8, 2007 ok everything is working great with switch($row_userinfo['level']){ case 1: $level = "User"; break; case 3: $level = "Admin"; break; default: $level = "User"; } echo $level; thank you all for your help i have learnt a lot of new stuff today thanks... Quote Link to comment https://forums.phpfreaks.com/topic/68441-solved-trying-to-create-search-page-warning-mysql_fetch_array-supplied-argument-is/#findComment-344165 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.