So, I want to get a column to a variable.
I tried 2 methodes
mysql_query
and
mysql_fetch_array
At mysql_query I get Resource id #5.
And
At mysql_fetch_array I get array.Does some one knows how to fix this?
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
Suppose you have column with name as 'firstname'. Then you can go with this
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$firstname = $userRow[0]['firstname'];
echo $firstname;
If you get multiple rows in result, you can get all rows by loop.
My understanding is that you want to call the column name from a table where the value of that column is equal to a variable? If so...
The easiest way I can think of to resolve this would be to so a search through all the tables for the value you look for based on columns and return the value. This function should help accomplish this task:
function columnToVariable($conn, $table, $variable){//SET PARAMETERS (CONNECTION STRING, TABLE NAME, VARIABLE SEARCH)
$sql = "SHOW COLUMNS FROM ".mysqli_real_escape_string($conn, $table).";"; // SQL STATEMENT
$result = $conn->query($sql); // RESULTS OF ALL ROWS
$field = array(); // ARRAY WITH COLUMNS
if($result->num_rows > 0){ // CHECK IF RESULTS FOUND
while($row = $result->fetch_assoc()){ // SET RESULTS TO ARRAY
array_push($field, $row['Field']); // APPEND ARRAY WITH FIELD NAME
}
}
foreach($field as $return){ // ITERATE THROUGH FIELD NAMES
$sql = "SELECT * FROM ".mysqli_real_escape_string($conn, $table)." WHERE ".mysqli_real_escape_string($conn, $return)." = '".mysqli_real_escape_string($conn, $variable)."';"; // SQL STRING
$result = $conn->query($sql); // QUERY RESULTS
if($result->num_rows > 0){ // IF FOUND, RETURN VALUE
return $return; // RETURN VALUE
}
}
}
Related
My variables in the mysql table are "usersEmail" "usersRefer and "usersPoint". I want to check the whole table if usersEmail = usersRefer on different rows then UPDATE the integer usersPoint on the row of both usersEmail and usersRefer. I want to check for one specific user, that is the value of the usersRefer checks the entire column of usersEmail to check if it matches.
Here's something that I tried out:
$sql = "SELECT * FROM walitlist";
if($result = $conn->query($sql)){
while ($row = $result->fetch_assoc()){
$pointSys = $row ["usersPoint"];
if ($row ["usersEmail"] = $row ["usersRefer"]){
$pointSys = "UPDATE waitlist SET usersPoint = usersPoint+100";
}
}
}
How can I make this work?
I am trying to fetch user
function getItemName($dbh, $userId) {
$itemId = getItemId($dbh, $userId); // the getItemId() function works
echo "item id is: " . $itemId ; // because I can see the correct result if I echo it
$sql = "SELECT name FROM items WHERE id = :item_id";
$stm = $dbh->prepare($sql);
$stm->bindParam(':item_id', $itemId, PDO::PARAM_INT);
$stm->execute();
$result = $stm->fetch();
return $result['name'];
}
And I get Trying to access array offset on value of type bool on the return $result['name']; line.
The field name exists on the items table so that's not the issue.
Also, when I try to further test it, I change the $sql statement to SELECT * FROM items and then when I do echo $stm->rowCount() it finds the correct number of rows (With the original SQL statement row count is 0)
Can't find out what's causing this
I have 3 suggestions:
Make sure to convert $itemId to integer using intval();
Just before returning the function result validate that the query returned results.
$result = $stm->fetch();
if(!$result){
return null;
}
return $result['name'];
Finally, the more obvious, make sure the itemId you are looking for exists in the DB.
I am trying to perform a query inside a while loop that is getting values from a column. I am trying to query first to get all my values from a column in my DB and then get the count of how many times that value is in that column.
Examples of output trying to get
myValue is in the column 3 times
myOtherValue is in the column 10 times
myOtherOtherValue is in the column 22 times
Example of code
$sql = "SELECT DISTINCT id, columnName FROM tableName";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
function myCount($id)
{
$query = "SELECT COUNT(*) FROM tableName WHERE name = '$id'";
$result = mysql_query($query);
$count = mysql_fetch_array($result);
}
echo "$id is in the column $count[0] times";
}
You can't define a function inside the while loop.
Using COUNT(*) with a GROUP BY name clause, then you could solve the problem with only one query.
say I have a variable
$id = mt_rand();
how can I query the mysql database to see if the variable exists in the row id, if it does exist then change the variable $id, once the variable is unique to all other stored ids, then insert it into the database?
Thanks you guys.
$con = mysql_connect("<host>","<login>","<pass>");
if ($con) {
mysql_select_db('<schemata>', $con);
$found = false;
while (!$found) {
$idIamSearching = mt_rand();
$query = mysql_query("SELECT count(*) FROM <table> WHERE <idColumnName>='".$idIamSearching."'");
$result = mysql_fetch_row($query);
if ($result[0] > 0) {
mysql_query("INSERT INTO <table> (<column>) VALUES ('".$idIamSearching."')");
$found = true;
}
}
mysql_close($con);
}
Your description is hard to understand, so, this is something that could give you pointers...
'SELECT COUNT(*) as count from table where row_id="'.$variable.'" LIMIT 1'
make sure to escape the variable if it's user input or if it's going to have more than alphanumeric characters
then fetch the row and check if count is 1 or greater than 0
if one, then it exists and try again (in a loop)
although, auto increment on the id field would allow you to avoid this step
$bExists = 0;
while(!$bExists){
// Randomly generate id variable
$result = mysql_query("SELECT * FROM table WHERE id=$id");
if($result){
if(mysql_num_rows($result) > 0){
$bExists = 1;
} else {
// Insert into database
$bExists = 1;
}
}
1 Randomly generate id variable
2 Query database for it
2.1 Result? exit
2.2 No result? Insert
How to check with php/sql if any of fields in database that are selected in while loop is 0?
$res = mysql_query('SELECT id, name FROM table'); \\check here?
while($row = mysql_fetch_array)
{
\\or check here?
}
Thanks in advance!
EDIT:
I need to select all fields and then check if any one of them is 0.
$foundZero = false;
$res = mysql_query('SELECT id, name FROM table');
while ($row = mysql_fetch_array($res))
{
if (in_array(0, $row))
{
// This row has a zero
$foundZero = true;
}
}
if ($foundZero)
{
// at least one zero in one row has been found
}
else
{
// No zeros have been found
}
If $foundZero is true, then at least one field in one row is equal to 0. Otherwise, all fields are non-zero.
in the query add a where clause
SELECT id, name FROM table where my_field=0
then you not need to check every results, you get only the wanted results rows.
Obviously, check inside the while loop. For the query may return ZERO row.
$res = mysql_query('SELECT count(*) FROM table WHERE my_field=0'); \\check here?
if(mysql_num_rows($res) > 0)
{
while($row = mysql_fetch_array)
{
\\or check here?
}
}
If you really have to return all rows and then check for zero, add an order by clause to your query to minimize your checking.
SELECT id, name FROM table ORDER BY id DESC
then
if ($id <= 0)
handleIt()
else
proceed()