Last active
October 3, 2024 02:33
-
-
Save subrotoice/affeba9b5d476e3be75e07eeb9a3f147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MySqli - OOP version Connection and PHP Scrip--------(mysqli)------------ | |
$host = "localhost"; | |
$username = "root"; | |
$password = ""; | |
$database = ""; | |
$conn = new mysqli($host, $username, $password, $database); | |
$sql = 'SELECT * FROM employee'; | |
$result = $conn->query($sql); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
var_dump($row); | |
} | |
} else { | |
echo "0 results"; | |
} | |
$conn->close(); | |
// mySQLi One line | |
$gptapi = $conn->query("SELECT description FROM gptapi WHERE id=52")->fetch_assoc()['description']; | |
// In One line result | |
$rate = mysql_fetch_array(mysql_query("SELECT rate FROM userPermission WHERE userid='3339'" , $conexion))['rate']; | |
$result = mysql_query("SELECT * FROM table1", $conexion); | |
$num_rows = mysql_num_rows($result); | |
// Query Chaining Fetching from 3 table | |
SELECT * FROM userPermission WHERE userid=(SELECT clave FROM usuario WHERE email=(SELECT salesPerson FROM tracking_Payment WHERE userid=11155)); | |
# PDO Version ########### Connection and PHP Script----------------------- | |
try { | |
$pdoCon = new PDO( "mysql:host={$host}; dbname={$database}", $username, $password ); | |
} | |
// show error | |
catch( PDOException $exception ){ | |
echo "Connection error: " . $exception->getMessage(); | |
} | |
# Simple Query execute | |
$sql = "SELECT * FROM students ORDER BY id DESC"; | |
$result = $pdoCon->prepare($sql); // PDO Connection Object | |
$result->execute(); | |
$catalog = $result->fetchAll(); | |
# Only field value | |
$sql= "SELECT image, personid FROM orders WHERE id=:id"; | |
$result = $pdoCon->prepare($sql); | |
$result->bindParam(':id', $OrderID); | |
$result->execute(); | |
$Order = $result->fetch(PDO::FETCH_ASSOC); | |
$OrderImage = $Order['image']; | |
# Update Value | |
$sql = "UPDATE users SET valuesetbyuser = $rate where username='$userName'"; | |
$result = $pdoCon->prepare($sql); | |
$result->execute(); | |
# Insert Data | |
$query = "INSERT INTO students SET name=:name, email=:email, age=:age, sex=:sex, hobby=:hobby, address=:address, image=:image, created=:created"; | |
# prepare query for execution | |
$stmt = $pdoCon->prepare($query); | |
# bind the parameters | |
$stmt->bindParam(':name', $name); | |
$stmt->bindParam(':email', $email); | |
$stmt->bindParam(':age', $age); | |
$stmt->bindParam(':sex', $sex); | |
$stmt->bindParam(':hobby', $hobby); | |
$stmt->bindParam(':address', $address); | |
$stmt->bindParam(':image', $fileName); | |
// specify when this record was inserted to the database | |
$created=date('Y-m-d H:i:s'); | |
$stmt->bindParam(':created', $created); | |
# Execute the query | |
if( $stmt->execute() ) { | |
$lastInsertId = $pdoCon->lastInsertId(); | |
echo "<div class='alert alert-success'>Record was saved. <a href='details.php?id=". $lastInsertId ."'>View Record</a></div>"; | |
} else { | |
echo "<div class='alert alert-danger'>Unable to save record.</div>"; | |
} | |
# Mysql version ### Update version might not working-------(mysql)-Update version might not working------------ | |
$conn = mysql_connect($host, $username, $password); | |
if(! $conn ) { | |
die('Could not connect: ' . mysql_error()); | |
} else { | |
echo "connected Succefully"; | |
} | |
mysql_select_db($database); | |
$sql = 'SELECT * FROM employee'; | |
$result = mysql_query( $sql, $conn ); // Esecute the query | |
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { | |
var_dump($row); | |
} | |
mysql_close($conn); | |
# Insert or Update | Count rows | |
$sql = "SELECT * FROM calenderapi WHERE userid=$userId"; | |
$result = mysql_query($sql, $conexion); | |
$num_rows = mysql_num_rows($result); | |
if($num_rows<=0) { | |
$sql = "INSERT"; | |
} else { | |
$sql = "UPDATE"; | |
} | |
# Sql Query | |
# Join between two table and three table | |
SELECT usuario.clave, moderador, price, paypalEmail FROM ((usuario INNER JOIN landing_page ON template=landing_page.clave) INNER JOIN campana ON landing_page.moderador = campana.moderator) WHERE usuario.clave=5412; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment