I'm brand new to both php and Mysql and i've got to the point where i need help . . . . . . . . .
I have built a MySQL database along with a php web page to access it. The problem i'm having is getting the php page to search the database. I can connect to the database and the table i've built, but not search it. I've spent the last 4 days trying a number of different php scripts, but to no avail. I have used the following code to access it . . . . . .
<?php
mysql_connect("HOST NAME", "USER NAME", "PASSWORD") or die(mysql_error());
echo "Connected to MySQL
";
mysql_select_db("DATABASE NAME") or die(mysql_error());
echo " Connected to Database";
?>
Newbie needing HELP !!!!!!
Moderator: General Moderators
-
tigger bounce
- Forum Newbie
- Posts: 3
- Joined: Fri Mar 18, 2011 10:31 am
Re: Newbie needing HELP !!!!!!
the above code only connects and selects the db..it does not do any queries....use mysql_query() and use mysql_fetch_assoc()...to get the result...you can use any fetch method of your choice...
-
tigger bounce
- Forum Newbie
- Posts: 3
- Joined: Fri Mar 18, 2011 10:31 am
Re: Newbie needing HELP !!!!!!
I've used several different codes, but nothing has worked. The last code i tried was . . . . .
// Get all the data from the "1939" table
$result = mysql_query("SELECT * FROM `1939`")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Date</th> <th>Serial</th> <th>Location</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['date'];
echo "</td><td>";
echo $row['serial'];
echo "</td></tr>";
echo $row['location'];
echo "</td></tr>";
}
echo "</table>";
As i've said in the original post, i do not have a clue about php etc, so i've been relying on what i can find on the internet and trying to adapt it as best i can
// Get all the data from the "1939" table
$result = mysql_query("SELECT * FROM `1939`")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Date</th> <th>Serial</th> <th>Location</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['date'];
echo "</td><td>";
echo $row['serial'];
echo "</td></tr>";
echo $row['location'];
echo "</td></tr>";
}
echo "</table>";
As i've said in the original post, i do not have a clue about php etc, so i've been relying on what i can find on the internet and trying to adapt it as best i can
-
tigger bounce
- Forum Newbie
- Posts: 3
- Joined: Fri Mar 18, 2011 10:31 am
Re: Newbie needing HELP !!!!!!
Right i've managed to access the database now . . . . . to a point
The idea is to type a word in a search box on a php page, then the results are displayed in a table. The problem now is it's showing the entire contents of the table, and there not in a table. So i've now got to the point where i don't no what to do/try next. This is the code i have so far . . . . . . . .
The idea is to type a word in a search box on a php page, then the results are displayed in a table. The problem now is it's showing the entire contents of the table, and there not in a table. So i've now got to the point where i don't no what to do/try next. This is the code i have so far . . . . . . . .
Code: Select all
<?php
mysql_connect("HOST NAME", "USER NAME", "PASSWORD") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("DATABASE NAME") or die(mysql_error());
echo " ";
// Retrieve all the data from the "1939" table
$result = mysql_query("SELECT * FROM `1939`")
or die(mysql_error());
// store the record of the "1939" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "Date: ".$row['data'];
echo " Serial: ".$row['serial'];
echo "Location: ".$row['location'];
// Make a MySQL Connection
$query = "SELECT * FROM `1939`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['Date']. " - ". $row['Serial']. "_". $row['Location'];
echo "<br />";
}
// Get all the data from the "1939" table
$result = mysql_query("SELECT * FROM `1939`")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Date</th> <th>Serial</th> <th>Location</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['Date'];
echo "</td><td>";
echo $row['Serial'];
echo "</td><td>";
echo $row['Location'];
echo "</td></tr>";
}
echo "</table>";
// Insert a row of information into the table "1939"
$result = mysql_query("SELECT * FROM `1939` WHERE age LIKE '%' ")
or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row
echo $row['Date']. " - ". $row['Serial']. "_". $row['Location'];
echo "<br />";
}
?>Re: Newbie needing HELP !!!!!!
What you're missing is the concept of a query and how to express it in SQL. It's quite simple, you've already got all the hard part done. Just read a few tutorials on SQL, such as http://www.firstsql.com/tutor2.htm. Your script seems to be doing the same thing over and over again, I don't understand what you are trying to do. And your comments are totally inappropriate for what the code is actually doing. I suspect that you have copied code from somewhere and just run it all together. That's a sure path to total confusion.
Just try to do one simple thing at a time, until you begin to understand how this all works. You have already seen how to connect to your database. Now try to just list everything in the table. When you understand how to do that, remove (or comment out) the code that issues that query and prints out those results, then try to search for a single row, or perhaps any rows that meet some condition (the WHERE clause) and print those results.
Just try to do one simple thing at a time, until you begin to understand how this all works. You have already seen how to connect to your database. Now try to just list everything in the table. When you understand how to do that, remove (or comment out) the code that issues that query and prints out those results, then try to search for a single row, or perhaps any rows that meet some condition (the WHERE clause) and print those results.