php and mysql query

flenters

Well-Known Member
Joined
Oct 14, 2005
Messages
341
Reaction score
15
Hi guys and help would be appreciated just before hand I am not php expert :D.
I have a small dbase (mysql) that has 3 fields make, model and scan code. So what I want to do is
Run a query with php to select the make and model then istshould display the scan code if that makes any sense.

So currently I have the following:
<?php
Mysql_connect(“localhost”, “whatever”, “morepasswd”) or die(mysql_error());
Mysql_select_db(“test”) or die(mysql_error());
$data = mysql_query(“SELECT * FROM test.table”) or die(mysql_error());
Print “<table border cellpadding=3>”;
While ($info = mysql_fetch_array( $data ))
{
Print “tr”;
Print “<th>Make:</th> <td>”.$info[‘MAKE’] .”</td>”;
}
?>

So this will print all the makes , I am not sure how to code the form function to only look for
A certain make and model ?

Any help / suggestion would be appreciated.
 
o.k how about

<?php
Mysql_connect(“localhost”, “whatever”, “morepasswd”) or die(mysql_error());
Mysql_select_db(“test”) or die(mysql_error());
$data = mysql_query(“SELECT * FROM test.table”) or die(mysql_error());
$sql = “SELECT * FROM test.table” . $_GET['MAKE'];
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query))
{
echo "<p>", $row[MAKE];
}
?>


This works and displays but i want a form to search the result and and only display the searched item O_o
 
Then why not add a filter to your select query, based on the fields being searched and the value that was searched for?

SELECT * FROM test.table WHERE test.table.field = %search.value%
 
Do not forget to use lower case search. I use postgresql and cannot remember what mysql users use.

ie SELECT * FROM table_x WHERE col_1 ILIKE '%koos%';

or you can use the "~~*" shorthand as ILIKE is not in mysql
 
Just a word of advice, if you ever ever want to do an update or delete - please just remember the WHERE clause. I forgot it once and ended up with an entire table of 300+ records all identical...
 
Just a word of advice, if you ever ever want to do an update or delete - please just remember the WHERE clause. I forgot it once and ended up with an entire table of 300+ records all identical...

Correct way to do update or deletes is with START TRANSACTION and COMMIT commands.
 
haha.. a good tip though is write ur select statement first with the where clause, then when u do your update you just copy past the where from the select. that way you know damn sure that it will only update what you selected before hand
 
Ok, lets see if I can help you (if you still need help though...)

Code:
<?php

$conn = mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("yourDB") or die(mysql_error());
$sql = "select * from yourTable"; // this will select every record in your table
$rs = mysql_query($sql) or die(mysql_error());

?>

<table border="1" align="center">
<tr><th>Make</th><th>Model</th><th>Serial Code</th></tr> 

<?php
while($record = mysql_fetch_array($rs))
         echo("<tr><td>".$record["make"]."</td><td>".$record["model"]."</td><td>".$record["code"]."</td></tr>");

mysql_close($conn);
?>
</table>
</body>
</html>


Or pretty much something in that line! If you want to select just certain models or makes get a little form that requests which ones you want to see and then amend your SQL query to reflect that...

Hope this helps...
 
I have a small dbase (mysql) that has 3 fields make, model and scan code. So what I want to do is
Run a query with php to select the make and model then istshould display the scan code if that makes any sense.

Just help me understand this part better. Do you already have a place where you enter the make and model then php should give you the scan code or do you just (like a report) want to see everything in your table?
 
Top
Sign up to the MyBroadband newsletter
X