bahaa
-
Posts
147 -
Joined
-
Last visited
Posts posted by bahaa
-
-
My sql query would be like this
$sql ="SELECT CategoryID, category.Title, Visible, Ordering, category_position.Title, CreationDate,
CreatedBy, LastEdited, LastEditedBy
FROM category
LEFT JOIN category_position USING(PositionID)
LEFT JOIN country_category USING(CategoryID)";
if($Visible ==1 OR $Visible ==1)
{
$sql .=" WHERE Visible = ? ";
}
$sql .=" ORDER BY Ordering ";
How do I do the binding if they user did not choose to sort the outpout ?
-
Thanks Larry
I have another question.
I have a page that display data from the database and I want to make sorting options by visibility and country.
How do I bind an optional parameter to the prepared statement ?
I know how to constrcut the the sql query but my problem is with the binding.
Here is my statement
function GetCategories()
{
global $mysqli;
$sql ="SELECT CategoryID, category.Title, Visible, Ordering, category_position.Title, CreationDate,
CreatedBy, LastEdited, LastEditedBy
FROM category
LEFT JOIN category_position USING(PositionID)
LEFT JOIN country_category USING(CategoryID)
ORDER BY Ordering ";
//prepare the stmt
$stmt = $mysqli->prepare($sql);
//execute the stmt
$stmt->execute();
//Store the result
$stmt->store_result();
//Bind the result to variables
$stmt->bind_result($CategoryID, $Title, $Visible, $Ordering, $Position, $CreationDate,
$CreatedBy, $LastEdited, $LastEditedBy);
//Get numRows
if($stmt->num_rows >0 )
{
//Fetch the array
while($stmt->fetch())
{?>
<tr>
<td><input type="checkbox" name="chkCategoryID[]" class="chk" value="<?php echo $CategoryID; ?>"/></td>
<td><?php echo $Title ;?></td>
<td><?php echo $Position ; ?></td>
<td>
<?php
if($Visible ==1)
{
echo '<img src="../images/icon-16-published.gif" width="16" height="16" alt="منشور" />';
}
else
{
echo '<img src="../images/icon-16-unPublished.gif" width="16" height="16" alt="منشور" />';
}
?>
</td>
<td><input type="text" name="txtOrdering" value="<?php echo $Ordering; ?>"/></td>
<td><?php echo $CreationDate; ?></td>
<td><?php echo $LastEdited; ?></td>
<td><?php echo GetUserByID($CreatedBy); ?></td>
<td><?php echo GetUserByID($LastEditedBy); ?></td>
</tr>
<?php }
}
//Close connection
$stmt->close();
//return the result
return $stmt;
}
-
Masterlayouts,
I am only inclined to relate the term transactions with something having to do with ecommerce. Does the transaction involve using the session id? It seems that when recording a post data (or many) per unique user, utilizing the session variable is required. Is it not?
The transactions are used if you want to inster multi record to the database and you want to make sure all queries succeed or roll back if something went wrong.
example of using transation is when transfering money from one account to another, if you send money to another acount and the money withdrown from you account and then something happens before the are deposited in the other account then everything roll back.
So in the bank account example when useing the transation is forcing to the queries to roll back if something goes wrong.
-
if the code you wrote it's correct than the function will return your $select, so I guess what you want is to display it in which case it should be as simple as
echo $select;
My function works, but what I want the fetching to happen out of the function not inside.
In the one above the fetching is happening inside the function and I call it on my page to dispay the drop down.
-
Thanks for all your responses.
To both Jonathan and Antonio, thanks for presenting the script in different ways. It helped me understand what was really going on behind the scenes. I could not understand it at first, I was also unsure about the way I comprehended it.
But to reiterate (directed at Antionio's explanation):
In it's most simplest form:
The variable takes on a new value each time it is passed through the ternary operator, correct?
Either way, I believe this is how I think it works. It makes sense.
@bahaa - Your suggestion also a worth considering in future projects. This method will be most widely used once CSS3 is most widely supported by all the browsers.
It is already supported by major browser and also on IE 9
- 1
-
I check for the ID like this if(isset($_SESSION['UserID'])){ $UserID = (int)$_SESSION['UserID']}
- 1
-
$sdbc is my connection.
The function is not missing any thing, I posted 2 functions, the one without id is used when sign in up for new user and in this case there is no id to check for.
the function that takes ID as a parameter is used when for updating.
-
Here is 2 functions to check for unique email address, one used for insert and the other for update
when you try to update a record and want to check for unique email, you have to check for all records except the one you are trying to use
function uniqueEamilUpdate($email, $ID){
global $sdbc;
$query = "SELECT userEmail, userID
FROM user
WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."'
AND userID !='". mysqli_real_escape_string($sdbc, $ID)."'";
$result = mysqli_query($sdbc, $query);
return $result;
}
and here is a function to check for unique email on insert
function uniqueEamil($email){
global $sdbc;
$query = "SELECT userEmail, userID
FROM user
WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."'";
$result = mysqli_query($sdbc, $query);
return $result;
}
-
I use the user session id to get the user id and add it to the table.
-
You don't need to use it anymore for alternat background color and you can use css instead
you can use this selector tr:nth-child(odd)
here is an example where I use it
#container #content-box .content-elements .tblListData tbody tr:nth-child(odd){
background: #fff;
border-top: 10px solid #FFF;
}
- 1
-
Hello,
I recently started using the mysqli prepared statement and I am not so good with them yet.
I have a mysqli prepared statement inside a function and it works fine but my problem is that I don't know how to fetch a mysqli prepared statement out side the function.
here is my function
function GetCountriesList()
{
global $mysqli;
//Build sql query
$sql ="SELECT CountryID, Name FROM country";
//Prepare the stmt
$stmt = $mysqli->prepare($sql);
//execute the stmt
$stmt->execute();
//store the result
$stmt->store_result();
//bind the result
$stmt->bind_result($CountryID, $Country);
$select= "<select name=\"cboCountry\">";
$select .="<option value=\"0\">الدولة</option>";
//fetch the result
while($stmt->fetch() )
{
$select .= "<option value=\"{$CountryID}\">{$Country}</option>";
}
$select .="</select>";
return $select;
}
How do I fetch the result out side the function ?
PHP/5.3.4
MySQL client version: mysqlnd 5.0.7-
-
What is the problem with the constraints?
Using the sql statement you provided, how would I get the online registered users and online guests? what should I do if I only want to get the online loggedin user in backened?
What If I want to get the name of the onlined logged in users?
If I want to know more about the strucuter of the whole database I would send it to you in private message.
-
I am using MySQL database and here is the session table
CREATE TABLE IF NOT EXISTS `session`(sessionID CHAR(32) NOT NULL,
sessionData MEDIUMTEXT NOT NULL,
sessionTime TIMESTAMP NOT NULL,
sessionGuest TINYINT UNSIGNED NOT NULL ,
userID INT UNSIGNED COMMENT 'Fk to the user table',
CONSTRAINT pk_session_sessionID PRIMARY KEY(sessionID),
CONSTRAINT fk_user_session_userID FOREIGN KEY(userID) REFERENCES user(userID),
INDEX(sessionTime),
INDEX(sessionGuest)
)ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
-
Can you give in an example if you got the time?
I want to be able to get the online guest and onlined logged in users.
-
Hi all,
I have used the tutorial to store session in the database, but I am not quite sure how to use it to insert user id and also get online users and online gues.
In the book you show how to delete the session when some one log out. How to we delete the session when the user close the browser without login out?
Thanks in advance
-
Yes, it is the time problem.
I set the time to a different time zone that the server uses.
I get my data insert with the date and time I want, but when I use now() in my query to select rows with them time I want, it uses the server time.
How do I fix this?
-
How do we get the length of a string without the spaces?
I want to count only the words without the spaces.
-
I know it is in the manual but the odd thing that it doesn't include today's post for some reason.
for example: if I have post with these dates 2011-11-22, 2011-11-24, 2011-11-19, 2011-11-18
It only select 2011-11-19, 2011-11-18 but not 2011-11-22
-
But WHERE articalPublishDate >= NOW() will select future date?
If I set an artical to be published in 2011-12-23 and use WHERE articalPublishDate >= NOW() then the artical will be published before 2011-12-23 comes
-
Hi all,
I have a table with these fields :articalID, articalTitle, articalBody, articalPhoto, articalTags, articalPublishDate, articalView, articalVisible, userID, catID
The articalPublishDate is a datetime type. FROM the CMS I set the date and times when the artical to be released or published. How do compare the articalPublishDate with the date and time now and select the row if date and time now equal or greater than the date and time in the table?
-
Hello,
How do we avoid cutting the last word when using the Left Or Right function in MYSQL to select a number x of characters from the database?
If it is not possible with the mysql, is there any way to do it in PHP ?
Bahaa,
-
Thanks for the quick response Larry.
-
Hello,
How do we output the dollar sign and format a number to a 2 decimal in mysql statement?
Bahaa,
-
So basically a query within a query? I suppose that would work, but I can't understand why you wouldn't just auto increment the ID field instead.
If I was working on this project, I would use the auto increment, but we are taking this in school and the teacher meant to not auto increment the ID field for teaching purposes , so she can show us how to use the sub query.
How To Fetch Mysqli Prepared Statement Out Side A Function
in PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
Posted
I know how to do the query but my problem is with this
$stmt->bind_param();
the number of variable and data type to bind it to the code above depend on the user, so I am not sure how many variable will be used.
How do I do it?