I Am having trouble with this, i have table from mysql named person with fields id, name, and person_room. I also have this table called room with fields of roomid, name and capacity. i want to call a data from field capacity of table room. but it shows Resource id #7
here is my code:
$check_room_capacity = "SELECT capacity from room WHERE roomid = '".$oroom."'";
$check_user_capacity = mysql_query($check_room_capacity);
$rows = mysql_fetch_array($check_user_capacity);
$check_room = "SELECT id from person WHERE person_room = '".$oroom."'";
$check_user_room = mysql_query($check_room);
if(mysql_num_rows($check_user_room) => $check_user_capacity){
echo "room is loaded please try another room";
}
//then code here to insert it if the number of person
//is less than the room capacity
I tried to echo this thing $check_user_capacity to know its value but it shows resource id#7. Do you guys have any idea?
The other thing is, why my php don't accept this operation '=>' and '<=' it always underlined in color red?
mysql_query() statement returns a resource pointer to the result set, not the data itself. You'll need to use mysql_fetch_array() in order to retrieve the actual data in the table.
$old_ip = mysql_query("SELECT current_ip FROM members WHERE id='$id'");
$row = mysql_fetch_array($old_ip);
$result_old_ip = $row['current_ip'];
Try this one:
$check_room_capacity = "SELECT capacity from room WHERE roomid = '".$oroom."'";
$check_user_capacity = mysql_query($check_room_capacity);
$rows = mysql_fetch_array($check_user_capacity);
$check_room = "SELECT id from person WHERE person_room = '".$oroom."'";
$check_user_room = mysql_query($check_room);
if(mysql_num_rows($check_user_room) >= $rows['capacity']){
echo "room is loaded please try another room";
}
Related
I've tried to get this to work in several ways, but I can't get this query to return the Cost value from the database to the $cost variable:
$query2 = "SELECT Cost FROM 'item' WHERE Item = '$item'";
$cost= $db->query($query2);
It seems to be empty when I try to echo it.
(The $item variable is selected from a dropdown list generated from the item-table in the mySQL-db. This works fine and if I echo the value from $item, it returns the name of the item as expected.)
Anybody sees what I'm doing wrong?
I could post my complete code if necessary, but I believe this explanation may be sufficient.
You have to declared 'fetch_array' and Make reference this URL : http://php.net/manual/en/mysqli-result.fetch-array.php
For Example:-
$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_NUM);
Change $query2 = "SELECT Cost FROM 'item' WHERE Item = '$item'"; to
$query2 = "SELECT Cost FROM item WHERE item = '".$item."'";
Assuming Cost is your column name, the first item is your table name, the second item is another column name (Please change your naming convention and not use the same name for table and column). To make your question clearer, please provide us with your table name, your column names so that we can give you a more accurate answer.
Also you will need to escape your $item variable by using '".$item."' so that you can query from your database proper using that variable.
You will then need to fetch the results from querying the database.
$results = $db->fetch_all($cost);
To test that the data were successfully fetched, you can test it by printing it using print_r($results); This should return you an array of the results.
This might help you.
$input = "100";
$query = "select sal from sal where sal='$input'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
//This will print sal
echo $row['sal'];
Please let me know if you've any questions.
$query=mysql_query("SELECT Cost FROM item WHERE Item = '$item'");
while($x=mysql_fetch_array($query))
{
$cost=$x['Cost'];
echo $cost; //here we can return the result
}
after searching for several questions, i'm still struggling with mysql queries in PHP, my current goal is to do a MYSQL query that counts how many repeated strings are in a column and then return this amount in a INT variable to be written in the database.
The current code looks like:
//Fetch value from form and uppercase the string
$glitter = utf8_decode(strtoupper($_POST['code_string']));
$magic = mysql_query("SELECT COUNT(*) FROM table WHERE CODE_STR = '$glitter'");
The next step is inserting the var $magic into a INT field in the database, however the value is always 0.
Where is my mistake?
Thanks.
mysql_query returns a resource on success, or FALSE on error.
try this
$magic = mysql_query("SELECT COUNT(*) as count FROM table WHERE CODE_STR = '$glitter'");
$row = mysql_fetch_assoc($magic);
$count = $row['count'];
Your approach is correct. What you need to do now is
Change your query from
$magic = mysql_query("SELECT COUNT(*) FROM table WHERE CODE_STR = '$glitter'");
to
$magic = mysql_query("SELECT COUNT(*) as total_num FROM table WHERE CODE_STR = '$glitter'");
mysql_fetch_assoc() Use the returned value from table
$magic_row = mysql_fetch_assoc($magic);
echo $magic_row['total_num'];
See
http://php.net/manual/en/function.mysql-fetch-assoc.php
i'am trying to get one cell from my mysql database.
this cell is unique because of the date and hour.
so if date and time is selected with a WHERE it should come back with one number.
I checked everything but it still doesnt work. i get zero returns.
does anyone see something wrong with my code?
$result2 = mysql_query("SELECT * FROM `chairs` WHERE `date` = '$date' AND `hour` = '$hour' ");
$row = mysql_fetch_array($result2);
$dbchairs = $row[$chairs];
echo $dbchairs;
You have undefined variable chairs in
$dbchairs = $row[$chairs];
If in DB is column called chairs, use:
$dbchairs = $row['chairs']; // key of $row array is column name
change this
$dbchairs = $row[$chairs];
to
$dbchairs = $row['chairs'];
You are using undefined variable $chairs as your index in $row array.
syntax: $row['name_of_the_column_in_database']
How do I echo the latest values in column1? The below code echos the values before the update.
while($line = mysql_fetch_array($result)) {
$Student = $line["calss8"];
$querySf = "SELECT SUM(ABC) AS val1 FROM tbl1 WHERE student = '$Student'";
$resultSf = mysql_query($querySf);
$rSf = mysql_fetch_array($resultSf);
$totalSf = $rSf['val1'];
$totTMonth = $totalSf;
mysql_query("UPDATE tbl4 SET column1 = $totTMonth WHERE student = '$Student' LIMIT 1");
}
echo $line["column1"].",,";
As far as I know, you'll have to make a separate query to see what was just updated. I mean, run your select, perform your update, then do another select. You can get general information like how many rows were updated, but I don't think you can get specific information like the changed values in a column. Phil was right in suggesting that you should just print out the '$totTMonth' value since that is what you are updating your column with. That would be less overhead than doing another query to the database.
I think that problem starts before the code above. This code line will display the select results :echo $line["column1"].",,";. The variable $line is set before the code above. My solution is to do the following:
$result1 = mysql_query("SELECT column1 FROM student ..."); /* I insert the select query here */
While($row= mysql_fetch_array($result)) {
echo $row['column1'].",,";
}
I am adding records to my database but when I reach 2 same records and add another same records it doubles the entry in the database.
Here is my code:
$q = mysql_query("SELECT * FROM grade1 WHERE G1StudNo = '$_POST[cat]' AND G1SCode = '$_POST[subcat]' ");
while($noticia2 = mysql_fetch_array($q)) {
if(empty($noticia2['G1Sem']) AND empty($noticia2['G1Year'])){
mysql_query("UPDATE grade1 SET G1Sem = '$_POST[Sem]', G1Year = '$_POST[Year]'
WHERE G1StudNo = '$_POST[cat]' AND G1SCode = '$_POST[subcat]'");
}
else {
$query = mysql_query("SELECT * FROM curriculum WHERE SCode='$_POST[subcat]'");
while($noticia = mysql_fetch_array($query)) {
$insertSQL1 = mysql_query("INSERT INTO grade1 (G1StudNo, G1SCode, G1Sem, G1Year, Semester, YearLevel)
VALUES ('$_POST[cat]','$_POST[subcat]','$_POST[Sem]','$_POST[Year]','$noticia[Semester]','$noticia[YearLevel]')");
}
}
}
The first obvious place to look is in the branch with the INSERT clause. My best guess is that the dataset returned from your select statement select * from curriculum ... is returning 2 records.
EDIT:
The only time the update clause is called is if both semester and year are empty, probably should be OR, since it is an invalid entry if either is empty. I don't understand the second part and why you are checking curriculum table
As far as the insert: it probably should only be used if the recordset from the first query finds no student record. If I am reading it right: currently it is called only if a record IS found but the semester and year are empty.