Initial C and PHP skeleton.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 15 Dec 2017 15:34:43 +0000 (16:34 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 15 Dec 2017 15:34:43 +0000 (16:34 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
.gitignore [new file with mode: 0644]
TD5/C/Makefile [new file with mode: 0644]
TD5/C/exemple_mysql_libmysqlclient.c [new file with mode: 0644]
TD5/PHP/testmysql.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..435d966
--- /dev/null
@@ -0,0 +1,25 @@
+*.static
+*.dynamic
+# for cygwin
+*.exe
+
+*.o
+
+*.so
+*.dylib
+*.a
+# for cygwin
+*.dll
+*.dll.a
+
+# atom linter
+*.gch
+
+# editor trash
+*.swp
+*~
+
+.build
+
+thumbs.db
+*.DS_Store
diff --git a/TD5/C/Makefile b/TD5/C/Makefile
new file mode 100644 (file)
index 0000000..b5cebf8
--- /dev/null
@@ -0,0 +1,82 @@
+# Sample Makefile to build simple project.
+#
+# This Makefile expect all source files (.c) to be at the same level, in the
+# current working directory.
+#
+# It will automatically generate dependencies, compile all files, and produce a
+# binary using the provided name.
+#
+# Set BINARY_NAME to the name of the binary file to build.
+# Set BUILD_TYPE to either debug or release
+#
+# Automatic dependencies code from:
+# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
+BINARY_NAME=infovols
+BUILD_TYPE=debug
+#BUILD_TYPE=release
+
+# ====================================
+# DO NOT CHANGE STUFF BEYOND THIS LINE
+# ====================================
+
+all: $(BINARY_NAME)
+
+CC=gcc
+LD=gcc
+
+WARN_FLAGS = -Wall -Wextra
+STD_FLAG = -std=c11
+
+ifeq ($(BUILD_TYPE),debug)
+BUILDDIR := .build/debug
+DEBUG_FLAG = -g
+DEBUG = 1
+STRIP_FLAG =
+OPTI_FLAG = -O0
+else
+BUILDDIR := .build/release
+DEBUG_FLAG =
+DEBUG = 0
+STRIP_FLAG = -s
+OPTI_FLAG = -O3
+endif
+
+CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
+LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
+
+OBJDIR := $(BUILDDIR)/objs
+$(shell mkdir -p $(OBJDIR))
+
+SRCS=$(wildcard *.c)
+OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
+
+DEPDIR := $(BUILDDIR)/deps
+$(shell mkdir -p $(DEPDIR))
+DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
+POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
+
+$(BINARY_NAME): $(OBJS)
+       @echo "[LD ] $@"
+       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+
+$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
+       @echo "[C  ] $*"
+       @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
+       @$(POSTCOMPILE)
+
+$(DEPDIR)/%.d: ;
+
+.PRECIOUS: $(DEPDIR)/%.d
+
+include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
+
+clean:
+       @echo "[CLN]"
+       -@rm -r $(BUILDDIR)
+       -@rm $(BINARY_NAME)
+
+disassemble: $(BINARY_NAME)
+       objdump -d $< | less
+
+symbols: $(BINARY_NAME)
+       objdump -t $< | sort | less
diff --git a/TD5/C/exemple_mysql_libmysqlclient.c b/TD5/C/exemple_mysql_libmysqlclient.c
new file mode 100644 (file)
index 0000000..88e82da
--- /dev/null
@@ -0,0 +1,83 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <mysql/mysql.h>
+
+//il faut : libmysqlclient-dev
+
+//compilation : 
+// gcc testsqlc.c -o testsqlc -I/usr/include/mysql -lmysqlclient -L/usr/lib64/mysql
+
+//execution :
+//./testsqlc
+
+
+int main(void) {
+      MYSQL *conn;
+      MYSQL_RES *res;
+      MYSQL_ROW row;
+      MYSQL_RES *result;
+
+      char *server = "";
+      char *user = "";
+      char *password = "";
+      char *database = "";
+
+      conn = mysql_init(NULL);
+
+      /* Connect to database */
+      if (!mysql_real_connect(conn, server,
+            user, password, database, 0, NULL, 0)) {
+          fprintf(stderr, "%s\n", mysql_error(conn));
+          exit(1);
+      }
+
+      /* send SQL query */
+      if (mysql_query(conn, "show tables")) {
+          fprintf(stderr, "%s\n", mysql_error(conn));
+          exit(1);
+      }
+
+      res = mysql_use_result(conn);
+
+      /* output table name */
+      printf("MySQL Tables in mysql database:\n");
+      while ((row = mysql_fetch_row(res)) != NULL)
+          printf("%s \n", row[0]);
+
+      /* query */
+      if (mysql_query(conn, "SELECT * FROM Usine")) 
+      {
+          fprintf(stderr, "%s\n", mysql_error(conn));
+          exit(1);
+      }
+  
+      result = mysql_store_result(conn);
+  
+      if (result == NULL) 
+      {
+          fprintf(stderr, "%s\n", mysql_error(conn));
+          exit(1);
+      }
+
+      int num_fields = mysql_num_fields(result);
+      int i;
+
+      printf("\nTable Usine :\n");
+      printf("NU\tNomU\tVille\n");
+
+      while ((row = mysql_fetch_row(result))) 
+      { 
+          for(i = 0; i < num_fields; i++) 
+          { 
+            printf("%s\t", row[i] ? row[i] : "NULL"); 
+          } 
+          printf("\n"); 
+      }
+
+      /* close connection */
+      mysql_free_result(res);
+      mysql_close(conn);
+
+      return 0;
+}
+
diff --git a/TD5/PHP/testmysql.php b/TD5/PHP/testmysql.php
new file mode 100644 (file)
index 0000000..8eec3f2
--- /dev/null
@@ -0,0 +1,33 @@
+<html>
+<body>
+<?php
+if( $id = mysql_connect($_SERVER['dbHost'],$_SERVER['dbLogin'],$_SERVER['dbPass']) ) {
+  if( $id_db = mysql_select_db($_SERVER['dbBd']) ) {
+       echo 'Connexion a la base.<br><br>';
+       $requete = "SELECT NumCl, NomCl, CodePosteCl, VilleCl FROM CLIENTS";
+       if($result = mysql_query($requete)) {
+               echo "<table border=1>\n";
+               echo "<tr><td>Code_CL</td><td>Nom</td><td>Rue</td><td>Ville</td></tr>\n";
+               while($ligne = mysql_fetch_row($result)) {
+                       $code = $ligne[0];
+                       $nom = $ligne[1];
+                       $rue = $ligne[2];
+                       $ville = $ligne[3];
+                       echo "<tr><td>$code</td><td>$nom</td><td>$rue</td><td>$ville</td></tr>\n";
+               }
+               echo "</table>\n";
+       } else {
+               echo 'Erreur de requete';
+       }
+  } else {
+       die('Echec de connexion a la base');
+  }
+  mysql_close();
+  echo '<br>Deconnexion.';
+} else {
+ die('Echec de connexion au serveur de bases de donnees');
+}
+?>
+</body>
+</html>
+