senyo Posted January 11, 2010 Share Posted January 11, 2010 Hi I am stupid and I can't search a database, everything I tried doesn't work, I want to search the table client, field name. Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/ Share on other sites More sharing options...
RaythMistwalker Posted January 11, 2010 Share Posted January 11, 2010 Hi I am stupid and I can't search a database, everything I tried doesn't work, I want to search the table client, field name. mysql_connect(HOST, USERNAME, PASSWORD); mysql_select_db(DB NAME) $qry = "SELECT name FROM client"; $result = mysql_query($qry); Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993017 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 I am using this but I am getting a blank screen mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $qry = "SELECT FROM client where client.name like '%".$each."%' "; $result = mysql_query($qry); echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993033 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 I have this error: You 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 client where name like john' at line 1 What does it mean? Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993041 Share on other sites More sharing options...
RaythMistwalker Posted January 11, 2010 Share Posted January 11, 2010 mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $qry = "SELECT FROM client WHERE client.name LIKE '%".$each."%' "; $result = mysql_query($qry); echo $result; WHERE and LIKE have to be in caps. Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993049 Share on other sites More sharing options...
Maq Posted January 11, 2010 Share Posted January 11, 2010 mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $qry = "SELECT FROM client WHERE client.name LIKE '%".$each."%' "; $result = mysql_query($qry); echo $result; WHERE and LIKE have to be in caps. No they don't. The problem is that you're not SELECTing anything. Moving to MySQL Help section. Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993052 Share on other sites More sharing options...
Lamez Posted January 11, 2010 Share Posted January 11, 2010 SELECT * FROM ... Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993055 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 how to finish the code so that it returns data from the database? Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993080 Share on other sites More sharing options...
Maq Posted January 11, 2010 Share Posted January 11, 2010 how to finish the code so that it returns data from the database? You need to provide the SELECT statement data to select. Read more here: http://dev.mysql.com/doc/refman/5.0/en/select.html Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993086 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 I have this and it shows no errors only this: Resource id #4Resource id #5 how to make it show the whole entry? mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $qry = "SELECT * FROM client WHERE client.name LIKE '%".$each."%'"; $result = mysql_query($qry); $eror= mysql_query($qry) or die(mysql_error()); $sebuty=$result['name']; echo $sebuty; echo $result; echo $eror; Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993099 Share on other sites More sharing options...
aebstract Posted January 11, 2010 Share Posted January 11, 2010 mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $result= "SELECT * FROM client WHERE client.name LIKE '%".$each."%'"; while($row = mysql_fetch_array($result)) { $sebuty= $row['name']; } echo $sebuty; echo $result; echo $eror; Need to fetch the information. http://php.net/manual/en/function.mysql-fetch-array.php Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993103 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 now I get this mysql_fetch_array() expects parameter 1 to be resource, string given in line 12 that is while($row = mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993113 Share on other sites More sharing options...
Maq Posted January 11, 2010 Share Posted January 11, 2010 $result= "SELECT * FROM client WHERE client.name LIKE '%".$each."%'"; while($row = mysql_fetch_array($result)) should be: $sql= "SELECT * FROM client WHERE client.name LIKE '%".$each."%'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) You should also add in some error handling. Read this blog for more details: http://www.phpfreaks.com/blog/or-die-must-die Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993116 Share on other sites More sharing options...
senyo Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks it work <?php mysql_connect("localhost","2","2"); mysql_select_db("book"); $each="john"; $sql= "SELECT * FROM client WHERE client.name LIKE '%".$each."%'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $fromdatabase= $row['name']; echo $fromdatabase; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993123 Share on other sites More sharing options...
aebstract Posted January 11, 2010 Share Posted January 11, 2010 Oh, doh.. forgot that line Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993124 Share on other sites More sharing options...
Maq Posted January 11, 2010 Share Posted January 11, 2010 Glad it's working. Keep in mind, if you're using user input in your query you must invoke mysql_real_escape_string on the value to prevent mysql injections. Quote Link to comment https://forums.phpfreaks.com/topic/188095-searching-a-mysql-database/#findComment-993135 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.