|
-
Jun 28th, 2007, 04:18 PM
#1
Thread Starter
Frenzied Member
how to connect to mysql and do a simple query
Ok, dumb question...
I just wanted to have 1 box, that searches the "zip code" filed in my database, and it will search and dispaly results of that, if records are found.
Can anyone give me an idea on how to do this?
ive looked all over online, and there are way to many examples, and most of them dont make sense.
Thanks
-
Jun 29th, 2007, 05:14 AM
#2
Re: how to connect to mysql and do a simple query
This is a simple example:
PHP Code:
<?php
header('Content-type: text/plain; charset=UTF-8');
$db = mysql_connect($hostname, $username, $password);
if ($db) {
mysql_select_db('DatabaseName', $db);
$result_set = mysql_query('SELECT zip_code FROM table_name', $db);
if (is_resource($result_set)) {
while ($row = mysql_fetch_assoc($result_set))
echo $row['zip_code']."\n";
}
else
echo 'Query failed: '.mysql_error($db);
}
else
echo 'Connection failed: '.mysql_error($db);
?>
In a production environment, you shouldn't use echo() to display all of the output, but rather wrap the PHP around your HTML:
PHP Code:
<h1>Zip Codes</h1>
<ol>
<?php while ($row = mysql_fetch_assoc($result_set)): ?>
<li><?php echo $row['zip_code'] ?></li>
<?php endwhile; ?>
</ol>
And once you have grasped this you should then move on to a data access library such as PDO or MySQLi, which offer parameterised queries. These are essential for any queries that use user input.
-
Jul 9th, 2007, 12:10 PM
#3
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
I was thinking... is there a way for it to check how many results are found?
Like if there is 1 result, have it do a redirect to a page, an if there are more then 1 result, then to kick out the results?
Also, where do i put the data for $databaseuser etc? Where do i define those, in a connection.php file?
I made a sameple a while back where i used another file, and then included that into my sheet taht was doing the querying.
thanks for all your help.
-
Jul 9th, 2007, 12:23 PM
#4
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
I was thinking... is there a way for it to check how many results are found?
Like if there is 1 result, have it do a redirect to a page, an if there are more then 1 result, then to kick out the results?
Also, where do i put the data for $databaseuser etc? Where do i define those, in a connection.php file?
I made a sameple a while back where i used another file, and then included that into my sheet taht was doing the querying.
thanks for all your help.
-
Jul 9th, 2007, 12:36 PM
#5
Re: how to connect to mysql and do a simple query
wouldn't it be easier to formulate the query like this?
PHP Code:
"SELECT zip_code FROM table_name WHERE zip_code LIKE $input_zip_code"
this would return 1 result or more, for which you could use the mysql_num_rows() function to find the number of results
Delete it. They just clutter threads anyway.
-
Jul 9th, 2007, 02:13 PM
#6
Re: how to connect to mysql and do a simple query
here is how you check how many rows you queried:
PHP Code:
$sql = "SELECT `col` FROM `table` WHERE `something`='test'";
$query = mysql_query($sql);
$num = mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT
if ($num == "1") {
//IT IS EQUAL TO 1 ROW
header("Location: newpage.php");
} else {
//IT IS GREATER THEN, OR LESS THEN 1
header("Location: otherpage.php");
}
My usual boring signature: Something
-
Jul 9th, 2007, 02:53 PM
#7
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
so if i have the rows: zip code, bus_name_, info1, and info2, zipcode, address, how can i displays those lines? on sepreate lines like
Joe's Busniess
Address, zip
notes1
thanks again everyone.
-
Jul 9th, 2007, 02:59 PM
#8
Re: how to connect to mysql and do a simple query
PHP Code:
$sql = "SELECT * FROM table_name WHERE zip_code LIKE $input_zip_code'";
$query = mysql_query($sql);
$num = mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT
if ($num == "1") {
$row = mysql_fetch_assoc($query);
$info1 = $row['columnName'];
//if you repeat this for each column you need, you have it all sorted out in variables.
} else {
//IT IS GREATER THEN, OR LESS THEN 1
header("Location: otherpage.php");
}
Delete it. They just clutter threads anyway.
-
Jul 9th, 2007, 06:45 PM
#9
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
is there a way to connect with a gui interface to my server that is hosted with maxdedicated?
ive always used a gui for mysql on my local machine, but never on a hosted server
-
Jul 9th, 2007, 06:46 PM
#10
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
where do i define databaseuser etc. at?
-
Jul 9th, 2007, 08:23 PM
#11
Re: how to connect to mysql and do a simple query
well if your server has cpanel, then you can access it from there, other wise you need to contact your host for that information. My guess would be through ssh connection
My usual boring signature: Something
-
Jul 10th, 2007, 12:02 AM
#12
Re: how to connect to mysql and do a simple query
PHP Code:
$db = mysql_connect($hostname, $username, $password);
mysql_select_db('DatabaseName');
Delete it. They just clutter threads anyway.
-
Jul 10th, 2007, 12:04 AM
#13
Re: how to connect to mysql and do a simple query
you might btw want to take a look at this site
http://www.php-mysql-tutorial.com/
the coding is pretty well explained
Delete it. They just clutter threads anyway.
-
Jul 10th, 2007, 12:36 PM
#14
Thread Starter
Frenzied Member
Re: how to connect to mysql and do a simple query
i wanted to make a page where when you go to it, you can put in a value into a text box, and then hit search, and then it searches, and displays the data...?
-
Jul 10th, 2007, 02:13 PM
#15
Re: how to connect to mysql and do a simple query
well thats what pena's code does, you just need to add a few more things. Do you want to use POST data or GET data for your search?
My usual boring signature: Something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|