Forums

Cannot make Mysql Query with Variables

erikym 11 Nov, 2008
Hi everyone I am trying to make a query with Variables. Which means someone chooses an option from a drop down list and with that value I get all the data from Mysql. I have tried so many things and had no success. Please help 😟 I am copying the php code I am using to execute this query. The rare thing is that when I hard code the variable after WHERE I get the expected result. HMMM!!

<?php

$name = "$_POST['airlines']";
$username = "myusername";
$password = "mypassword";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select database:" .mysql_error());

//execute the SQL query and return records

$result = mysql_query("SELECT * FROM airlines WHERE airline = '$name'");

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row=mysql_fetch_row($result);

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
echo $row[1];
echo $row[2];
echo $row[3];
?>
Max_admin 11 Nov, 2008
Hi erikym,

I think you better do this code the Joomla code space way or it may interfere with joomla and you may not get the expected results yes!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
erikym 11 Nov, 2008
Do you know of a place where I can learn this or read more about it or if you can post an example it would be greatly appreciated. I am kind of new to Joomla.
Max_admin 11 Nov, 2008
this is too easy, search the forums for "SELECT" and you should find tons of SELECT statements done the Joomla code way, be careful to see if its a J1.5 or J1.0 code, its always mentioned in the topic!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.