What is resource(150) of type (mysql result)? I am getting that after var_dumping an select query,
My code is:
$userlevelcheck = $this->query_silent("SELECT user_level FROM " . DB_PREFIX . "users WHERE user_id='" . $user_id . "'");
var_dump ($userlevelcheck);
if ($userlevelcheck != "1")
{
code
}
Try:
$userlevelquery = $this->query_silent("SELECT user_level FROM " . DB_PREFIX . "users WHERE user_id='" . $user_id . "'");
$row = mysql_fetch_assoc($query);
if ($row['user_level'] != "1")
{
code
}
You need to get the row with the data in ($row) then check the relevant field $row['user_level']
Related
I have the following SQL.
$sql_shells = "UPDATE `shells` SET `situation` = 'sold' WHERE `id` = '" . $id . "'";
$conn->query($sql_shells);
I updated the rows with column named buytime and another one called buyer so it would be like this:
$sql_shells = "UPDATE `shells` SET `buyer`= " . $buyer . ", `buytime`= " . $trn_date . " , `situation` = 'sold' WHERE `id` = '" . $id . "'";
$conn->query($sql_shells);
It's not updating.
`$trn_date = $trn_date = date("Y-m-d H:i:s");
and
$buyer = $_SESSION['username'];
Error log is empty.
Another SQL update doesn't have constant values and works fine
$sql_users = "UPDATE `users` SET `amount`= " . $newAmount . ", `gain`= " . $newgain . " WHERE `username` = '" . $buyer . "'";
I tried echoing the SQL query that's not working, this is what I get when I run it.
try to remove ` caracter and i think buytime and buyer should add ' caracter and remove it from id if is int type:
$sql_shells = "UPDATE shells SET buyer= '" . $buyer . "', buytime= '" . $trn_date . "' , situation = 'sold' WHERE id = " . $id ;
$conn->query($sql_shells);
ORDER BY is not working. I've tried ways I know but it won't order by, why?
returns : Call to a member function result_array() on boolean
public function getDetailbyClsandSection($class_id, $section_id, $exam_id) {
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session));
return $query->result_array();
}
$query = $this->db->query("SELECT exam_schedules.*,subjects.name,subjects.type FROM exam_schedules,teacher_subjects,exams,class_sections,subjects WHERE exam_schedules.hide = 'N' and exam_schedules.teacher_subject_id = teacher_subjects.id and exam_schedules.exam_id =exams.id and class_sections.id =teacher_subjects.class_section_id and teacher_subjects.subject_id=subjects.id and class_sections.class_id =" . $this->db->escape($class_id) . " and class_sections.section_id=" . $this->db->escape($section_id) . " and exam_id =" . $this->db->escape($exam_id) . " and exam_schedules.session_id=" . $this->db->escape($this->current_session) . " ORDER BY exam_schedules.date_of_exam ASC");
I'm working on a page, where users post their betting picks. In MySQL I have the table bets (id, event, pick, odds, stake, analysis, status, profit).
I would like to check if 'status' is empty in MySQL, but the if() statement is not working. If it's empty, it should output all the bets from a user. The code posted is in a for loop.
So what is wrong with the if() statement? And is there any better way to do this?
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
$row = mysqli_fetch_array($result);
if ('' !== $row['status']) {
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
You are using identical comparison, which would check for type & value both. === or !== as this involves comparing the type as well as the value.
Instead try -
if (!empty($row['status'])) { // assuming status would hold only strings (not false/0 etc)
Or
if ($row['status'] != '') {
Use mysqli_num_rows(). If its greater then 0 then we can say that query containing result so we can further proceed.
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_array($result);
if ($row['status'] != "") {
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
}
For status check you can use any of below method
if ($row['status'] != "") { }
OR
if (!empty($row['status'])) { }
If it's empty, it should output all the bets from a user. The code posted is in a for loop.
Since you are checking if it's empty, your if statement should be the other way round:
if ($row['status'] == '') {
Alternatively, you can use mysqli_num_rows() get the number of rows:
Returns the number of rows in the result set.
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
Also, there isn't such function called queryMysql():
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
It should be mysqli_query(). For mysqli_query(), the connection parameter is needed.
$result = mysqli_query($conn, "SELECT * FROM bets WHERE user='$user'");
$imagesarray[]=explode('&',$data[5]);
$imag_temp = 0;
foreach($imagesarray as $image){
if($imag_temp == 0 )
{
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = 'media/" . $image[0] . "' WHERE product_id = '" . (int)$product_id . "'");
}else{
foreach($image as $img){
$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET image = 'media/" . $img . "' , product_id = '" . (int)$product_id . "'");
}
}
$imag_temp++;
}
This is my $data[5]=Arizona Diamondbacks.png&arizona-cardinals.png&Atlanta Braves.jpg&...&...&...n number can have..
I have writend query for each for where..in that every first image should insert to one table and remaining all other images should insert to another table..now for every loop first image conidering as first /.
You don't need $imagesarray[] if this is for a single product.
Say
$data[5] = "Arizona Diamondbacks.png&arizona-cardinals.png&Atlanta Braves.jpg";
$imagesarray = explode('&',$data[5]);
Will create an array like
Array
(
[0] => Arizona Diamondbacks.png
[1] => arizona-cardinals.png
[2] => Atlanta Braves.jpg
)
Then using single variable assign the query to perform
foreach($imagesarray as $key => $image){
if($key == 0 )
{
$query = "UPDATE " . DB_PREFIX . "product SET image = 'media/" . $image . "' WHERE product_id = '" . (int)$product_id . "'";
} else {
$query = "INSERT INTO " . DB_PREFIX . "product_image SET image = 'media/" . $image . "' , product_id = '" . (int)$product_id . "'";
}
$this->db->query($query);
}
what is the error you are getting.. your insert query is not correct.
and other thing you have to notice is what type of array you are getting ...
You can edit this as required
this code is working you can modify with your insert and update query..
<?php
$data[5]='Arizona Diamondbacks.png&arizona-cardinals.png&Atlanta Braves.jpg';
$imagesarray[]=explode('&',$data[5]);
foreach ($imagesarray[0] as $key => $value) {
if($key==0){
echo "hello"; // here is your update query
}
else{
echo "sorry"; // here is your insert query
}
}
I don't know what you want, but you have some basic errors. You should learn more about php and sql before try to code something.
You don't need to add [] to the $imagesarray when explode the $data[5]
In the foreach loop the $image variable is a string, not an array
your INSERT query is incorrect, you should take a look to the documentation http://dev.mysql.com/doc/refman/5.7/en/insert.html
Your code should be something like this
$imagesarray = explode('&',$data[5]);
$imag_temp = 0;
foreach($imagesarray as $image){
if($imag_temp == 0 )
{
$this->db->query("UPDATE " . DB_PREFIX . "product SET image = 'media/$image' WHERE product_id = '$product_id'");
}else{
foreach($image as $img){
$this->db->query("INSERT INTO " . DB_PREFIX . "product_image (image, product_id) VALUES ('media/$image', '$product_id')");
}
}
$imag_temp++;
}
Make some research to learn how sql and php work, and make some debug because your code can have more bugs
How can I check if $user_id and $logged_user are in both the MySQL table fields userID and friendID
MySQL code.
$user_id = 2;
$logged_user = 1;
$dbc = mysqli_query($mysqli,"SELECT *
FROM users_friends
WHERE userID = '" . $logged_user . "'
AND friendID = '" . $user_id . "'");
if you want to check both combinations
SELECT *
FROM users_friends
WHERE (userID = '" . $logged_user . "'
AND friendID = '" . $user_id . "')
OR (friendID = '" . $logged_user . "'
AND userID = '" . $user_id . "')"
$dbc->num_rows
would be non-zero if the query succeeds and there's any matching rows in the table.
comment followup:
$dbh = new mysqli(...);
$sql = <<<EOL
SELECT *
FROM users_friends
WHERE (userID = $logged_user) AND (friendID = $user_id)
EOL;
$stmt = $dbh->query($sql)
if ($stmt->num_rows > 0) {
echo "That record exists";
}
You'd want to put in some error condition handling (connection failed, query failed, etc...) but that's the basics.