I have done this type of SELECT many times, but this time I can't get it to work. Any ideas, please?
$Name = "Dick";
$conn = mysqli_connect($server, $dbname, $dbpw, $dbuser);
$sql = "SELECT id FROM table WHERE $Name = table.first_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$customer_id = $row['id'];
Database::disconnect();
echo "customer id = " . $customer_id;
If you really DO have a table named table it would be more appropriate to use back ticks around the name since the word TABLE is a reserved word in MySQL. You should also use single quotes around your variable if it contains a string:
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
Other possible reasons if the query still doesn't work for you:
Make sure you have the connection parameters in the right order. It should be: mysqli_connect($server, $dbuser, $dbpw, $dbname).
You should be using fetch_array() instead of fetch_assoc() if you expect a one row result.
You are mixing PROCEDURAL STYLE with Object Oriented Style when using mysqli_connect() instead of mysqli(), at the same time using $result-> which is object oriented style. You should decide one style and stick with it.
This would be the procedural style of your query:
$Name = "Dick";
$conn = mysqli_connect($server, $dbuser, $dbpw, $dbname); // NOTE THE CHANGED ORDER OF CONNECTION PARAMETERS!
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
mysqli_free_result($result); // FREE RESULT SET
mysqli_close($conn); // CLOSE CONNECTION
And this would be the object oriented style:
$Name = "Dick";
$conn = new mysqli($server, $dbuser, $dbpw, $dbname);
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
$result->free(); // FREE RESULT SET
$conn->close(); // CLOSE CONNECTION
I would recommend naming your table something else than table since it's a reserved word and could get you into parsing problems. The same goes with field names. More reading: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
More about mysqli_fetch_array() and differences in procedural style and object oriented style use: http://php.net/manual/en/mysqli-result.fetch-array.php
$sql = "SELECT id FROM table WHERE '$Name' = table.first_name";
You simply need to concat the variable like this:
$sql = "SELECT id FROM table WHERE " . $Name . " = table.first_name";
Related
In this project, I have a dynamic columns which I stored in mapping table. Now usnig following query, I can fetch all the column names I need.
$db = new PDO("...");
$statement = $db->prepare("select column_name from mapping_table");
$list = $statement->fetch();
$matches = implode(',', $list);
Now from this result, I need to make a query. My question is how can I pass this $matches to the column_name of second query.
$db1 = new PDO("...");
$qry1= $db1->prepare("select {$matches} from table1");
$result= $qry1->fetch();
Try something like this:
db = new PDO("...");
$statement = $db->query("select column_name from mapping_table");
$list = $statement->fetchAll(PDO::FETCH_COLUMN);
$matches = implode('`,`', $list);
And then:
$db1 = new PDO("...");
$qry1= $db1->query("select `$matches` from table1");
$result= $qry1->fetch();
I am trying to work with a voting database system. I have a form to display all the candidates per candidate type. I'm still trying to to explore that. But this time, I want to try one candidate type, lets say for Chairperson, I want to display all candidate names for that type in the ballot form. However, there's an error with the line where i declare $query and made query statements, can somebody know what it is. I am very certain that my syntax is correct.
function returnAllFromTable($table) {
include 'connect.php';
$data = array ();
$query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1'; //ERROR
$mysql_query = mysql_query ( $query, $conn );
if (! $mysql_query) {
die ( 'Go Back<br>Unable to retrieve data from table ' . $table );
} else {
while ( $row = mysql_fetch_array ( $mysql_query ) ) {
$data [] = $row;
}
}
return $data;
}
As #Darwin von Corax says, I sure that you have a problem between $table and WHERE
Your query:
$query = 'SELECT * FROM ' . $table. 'WHERE candidateId=1';
If $table = 'Chairperson';
You have:
'SELECT * FROM ChairpersonWHERE candidateId=1';
The your query should be:
$query = 'SELECT * FROM ' . $table. ' WHERE candidateId=1';
Hi gus this is my first question in this coumunity i hope someone answer me
when i want to search from array of mysql_fetch_arrray he give me on error
this is the code
$key = "Meca";
$query = "SELECT * FROM subject WHERE `name` LIKE '%$key%' Or `sale` LIKE '%$key%' Or `Emphet` LIKE '%$key%' ";
$sql = mysql_query($query);
$num = mysql_num_rows($sql);
while ($num >0)
{
$row = mysql_fetch_object($sql);
$num--;
$row = mysql_fetch_object($sql);
in_array($row , "Meca");
}`
You are not clear with your question;
Anyway I give a try:
$key = "Meca";
$query = "SELECT * FROM subject WHERE name LIKE '%$key%' Or sale LIKE '%$key%' Or Emphet LIKE '%$key%'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if(in_array($row , "Meca")){
echo "$key is present this time."
}
}
Note that:
I am not on your requirement. So I just make a rough sketch.
You have not provided your error message.
You may have error in your code(SQl or PHP)[like usage of names
'Emphet'].
Anyway give a try.
I am using this query in Fabrik (Joomla Application) to pull the data from database, which is not working.
The same query with mysql syntax is working fine in PHPMYADMIN.
$db = FabrikWorker::getDbo(false, 2);
$query = $db->getQuery(true);
$query
->select('hostel_fee')
->from('hostel_fee AS a')
->join('INNER','students AS b ON (b.class = a.class)');
$db->setQuery($query);
$a = $db->loadResult();
return $a;
use the full queries like this
$db = &JFactory::getDBO();
$query = "SELECT m.id, m.title,m.level,mt.menutype FROM #__menu AS m
INNER JOIN #__menu_types AS mt ON mt.menutype = m.menutype
WHERE mt.menutype = m.menutype AND m.published = '1' ORDER BY mt.menutype,m.level";
$db->setQuery($query);
$rows = $db->loadObjectList();
OR
$rows = $db->loadResult();
Instead of using this
$db = FabrikWorker::getDbo(false, 2);
Use this.
$db =& JFactory::getDBO();
Or if you want to use any external database to connect to your extension you can use this
Connecting to an external database
So I have following as part of my query
SELECT * FROM $table WHERE columname LIKE '%$searchterm%'
I have tried taking out leading and/or ending wildcards meaning
SELECT * FROM $table WHERE columname LIKE '$searchterm%'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm%' OR columname LIKE '$searchterm'
and also tried adding following to the query with no luck
OR columname = '$searchterm'
So when my search term is "myval" and if column has whole string "myval", I would like to have that selected. But ALL of my queries above, return false/return nothing where myval is searchterm and column value as full.
I can not use MATCH because this is not Full-Text index.
EDIT:
PHP Code:
$sterm = NULL;
$table = 'mytable';
if(isset($_GET['s'])) { $sterm = explode(" ", mysql_real_escape_string($_GET['s'])); }
if(isset($_POST['s'])) { $sterm = explode(" ", mysql_real_escape_string($_POST['s'])); }
if(!empty($sterm)){
$getdata = "SELECT * FROM $table WHERE termsi != 'Special' ";
foreach ($sterm as $value){
$getdata .= "AND netid_all LIKE '%$value%' OR netid_all = '$value' ";
} //End foreach
$getdata .= "LIMIT 10";
$result = mysql_query($getdata) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo <<<PRINTALL
{$row[0]}, {$row[1]}, {$row[2]}, {$row[3]}, {$row[4]}, {$row[5]}, {$row[6]}, {$row[7]}, ' <br />'
PRINTALL;
} //End While
} //End If search exists
Okay So As you guys suggested, i tried PHPMyAdmin sql console and it works fine, so it would have to be by PHP!? so here it is.
I'd suggest writing your query building like this:
$fullvalues = array();
$partials = array();
foreach ($sterm as $value){
$partials[] = "(netid_all LIKE '%" . mysql_real_escape_string($value) . "%')";
$fullvalues[] = "'" . mysql_real_escape_string($value) . "'";
}
$partials = implode(' OR ', $partials);
$fullvalues = implode(', ', $fullvalues);
$sql = <<<EOL
SELECT *
FROM $table
WHERE (termsi != 'Special')
AND (($partials) OR (netid_all IN ($fullvalues));
EOL;
Assuming your search string is a b c, you'd get this query:
SELECT *
FROM yourtable
WHERE (termsi != 'Special')
AND (((netid_all LIKE '%a%') OR (netid_all LIKE '%b%') OR (netid_all LIKE '%C%')) OR (netid_all IN ('a', 'b', 'c')))
If your search requires that all terms be present, then change the 'OR' to 'AND' in the implode.
Well found it,
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
Was the problem, earlier when I was testing things, anyhow, it should have been the following
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row)